package com.youku.plugins.toolboxV4.model
{
   import flash.events.EventDispatcher;
   import flash.utils.Timer;
   import flash.net.URLLoader;
   import flash.net.URLRequest;
   import flash.events.TimerEvent;
   import flash.events.Event;
   import flash.events.IOErrorEvent;
   import flash.events.SecurityErrorEvent;
   import com.adobe.json.JSON;
   import com.youku.plugins.toolboxV4.ToolboxEvent;
   import flash.net.URLVariables;
   import flash.net.URLRequestMethod;
   
   public class CloudDataProxy extends EventDispatcher
   {
      
      public function CloudDataProxy() {
         super();
         this.init();
      }
      
      private static const TOTALRETRYTIMES:int = 2;
      
      private var _loadTimer:Timer;
      
      private var _urlLoader:URLLoader;
      
      private var _times:int;
      
      private var _currentRequest:URLRequest;
      
      private function init() : void {
         this._times = TOTALRETRYTIMES;
         this._loadTimer = new Timer(3 * 1000);
         this._loadTimer.addEventListener(TimerEvent.TIMER,this.onRetry);
         this._urlLoader = new URLLoader();
         this._urlLoader.addEventListener(Event.COMPLETE,this.onLoadComplete);
         this._urlLoader.addEventListener(IOErrorEvent.IO_ERROR,this.onRetry);
         this._urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onRetry);
      }
      
      private function onLoadComplete(param1:Event) : void {
         var obj:Object = null;
         var e:Event = param1;
         this._loadTimer.stop();
         var datastr:String = URLLoader(e.currentTarget).data;
         if(datastr == null || datastr == "")
         {
            this.onRetry(null);
            return;
         }
         try
         {
            obj = com.adobe.json.JSON.decode(datastr);
         }
         catch(error:Error)
         {
            obj = new Object();
            onRetry(null);
            return;
         }
         var cloudData:CloudData = new CloudData();
         if(obj.hasOwnProperty("code"))
         {
            cloudData.code = obj.code;
         }
         if(obj.desc)
         {
            cloudData.desc = obj.desc;
         }
         if(cloudData.code == 0)
         {
            dispatchEvent(new ToolboxEvent(ToolboxEvent.CLOUD_DATA_SET_SUCCESS,cloudData));
         }
         else
         {
            dispatchEvent(new ToolboxEvent(ToolboxEvent.CLOUD_DATA_SET_ERROR,cloudData));
         }
      }
      
      private function onError() : void {
         this._loadTimer.stop();
         try
         {
            this._urlLoader.close();
         }
         catch(e:Error)
         {
         }
         dispatchEvent(new ToolboxEvent(ToolboxEvent.CLOUD_DATA_SET_ERROR));
      }
      
      private function onRetry(param1:Event) : void {
         if(this._times < 1)
         {
            this.onError();
            return;
         }
         this._times--;
         this._urlLoader.load(this._currentRequest);
         this._loadTimer.reset();
         this._loadTimer.start();
         this.onError();
      }
      
      public function saveGlobalCloudData(param1:ToolboxData, param2:ToolboxData) : Boolean {
         this._times = TOTALRETRYTIMES;
         var _loc3_:* = "http://i.youku.com/user/cloudvarSet/";
         var _loc4_:* = false;
         if(!(param1.isContinuous == param2.isContinuous) || !(param1.isJump == param2.isJump) || !(param1.voice == param2.voice))
         {
            _loc4_ = true;
         }
         if(!_loc4_)
         {
            return false;
         }
         var _loc5_:URLRequest = new URLRequest(_loc3_);
         var _loc6_:Object = {};
         _loc6_.player_consequent = param1.isContinuous;
         _loc6_.player_skip_titles_credits = param1.isJump;
         _loc6_.player_language = encodeURIComponent(param1.voice);
         var _loc7_:URLVariables = new URLVariables();
         _loc7_.data = com.adobe.json.JSON.encode(_loc6_);
         _loc5_.data = _loc7_;
         _loc5_.method = URLRequestMethod.POST;
         this._currentRequest = _loc5_;
         this._urlLoader.load(_loc5_);
         this._loadTimer.reset();
         this._loadTimer.start();
         return true;
      }
   }
}
